/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.configuration;

import java.util.Arrays;

import org.sourceprojects.lycia.AbstractTagParser;
import org.sourceprojects.lycia.DefinitionBuilder;
import org.sourceprojects.lycia.Delegate;
import org.sourceprojects.lycia.ParserContext;
import org.sourceprojects.lycia.QualifiedName;
import org.sourceprojects.lycia.annotations.Tag;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.internal.validation.PropertyDescriptor;
import org.sourceprojects.lycia.internal.validation.ValidatingTagDefinitionParser;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * 
 * @author noctarius
 * @since 1.9.1
 * 
 * @param <B>
 */
public class DelegateTagDefinitionParser<B> extends AbstractTagParser<B>
		implements ValidatingTagDefinitionParser<B> {

	private final Delegate<B> delegate;
	private final String namespaceURI;
	private final String[] tags;
	private final Tag tag;

	public DelegateTagDefinitionParser(final Delegate<B> delegate,
			final Tag tag, final String namespaceURI) {

		this.delegate = delegate;
		this.namespaceURI = namespaceURI;
		this.tags = new String[] { tag.value() };
		this.tag = tag;
	}

	@Override
	public String getNamespace() {
		return namespaceURI;
	}

	@Override
	public String[] getTags() {
		final String[] result = new String[tags.length];
		System.arraycopy(tags, 0, result, 0, tags.length);
		return result;
	}

	@Override
	public Tag[] getTagAnnotations() {
		return new Tag[] { tag };
	}

	@Override
	public void parseElement(final Element element,
			final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {
		parseElement(element, null, null, parserContext, definitionBuilder);
	}

	@Override
	public void parseElement(final Element element, final String localName,
			final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {
		parseElement(element, localName, null, parserContext, definitionBuilder);
	}

	@Override
	public void parseElement(final Element element, final String localName,
			final QualifiedName parent, final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {
		delegate.invokeParse(element, localName, parent, parserContext,
				definitionBuilder);
	}

	@Override
	public Node transformElement(final Element element,
			final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {
		return transformElement(element, null, null, parserContext,
				definitionBuilder);
	}

	@Override
	public Node transformElement(final Element element, final String localName,
			final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {
		return transformElement(element, localName, null, parserContext,
				definitionBuilder);
	}

	@Override
	public Node transformElement(final Element element, final String localName,
			final QualifiedName parent, final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {
		return delegate.invokeTransform(element, localName, parent,
				parserContext, definitionBuilder);
	}

	public Delegate<B> getDelegate() {
		return delegate;
	}

	public Tag getTag() {
		return tag;
	}

	@Override
	public String toString() {
		return "DelegateTagDefinitionParser [delegate=" + delegate
				+ ", namespaceURI=" + namespaceURI + ", tag=" + tag + ", tags="
				+ Arrays.toString(tags) + "]";
	}

	@Override
	@SuppressWarnings("unchecked")
	public PropertyDescriptor<B>[] getPropertyDescriptor() {
		if (delegate instanceof ValidatingTagDefinitionParser<?>) {
			return ((ValidatingTagDefinitionParser<B>) delegate)
					.getPropertyDescriptor();
		}

		throw new UnsupportedOperationException(
				"Underlying method delegate is not capable of validating");
	}

}
